<template>
  <div class="articaldetail">
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" />
        <span class="iconfont iconnew new"></span>
      </div>
      <span :class="{ span: this.post.has_follow }" @click="user_follows">{{
        this.post.has_follow ? "已关注" : "关注"
      }}</span>
    </div>
    <div class="detail">
      <div class="title">{{ post.title }}</div>
      <div class="desc">
        <!--mounted和模板解析是同时的，所以此时 post.user=undefined 而undefined.会报错，解决方法1或者2-->
        <!-- 1 -->
        <span>{{ post.user && post.user.nickname }}</span> &nbsp;&nbsp;
        <span>{{ post.create_date | YTDdateFormat }}</span>
      </div>
      <!-- 解析文本的中的标签 -->
      <div class="content" v-if="post.type == 1" v-html="post.content"></div>
      <video
        class="content"
        v-else-if="post.type == 2"
        controls
        :src="axios.defaults.baseURL + post.content"
      ></video>
      <div class="opt">
        <span class="like" :class="{ unlike: post.has_like }" @click="giveLike">
          <van-icon name="good-job-o" />{{
            post.has_like ? post.like_length + 1 : post.like_length
          }}
        </span>
        <span class="chat"> <van-icon name="chat" class="w" />微信 </span>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item" v-for="item in comment" :key="item.id">
        <div class="head">
          <img :src="item.user.head_img" alt />
          <div>
            <p>{{ item.user.nickname }}</p>
            <span>{{ item.create_date | offsetTimeFormat }}</span>
          </div>
          <span>回复</span>
        </div>
        <div class="text">{{ item.content }}</div>
      </div>
      <div class="more">更多跟帖</div>
    </div>
    <!-- 评论 -->
    <commentFooter :post="post"></commentFooter>
  </div>
</template>

<script>
import axios from "@/utils/myaxios";
// 文章详情请求
import { getPostDetail, getLike, commentlist } from "@/apis/post";
// 过滤器
import { YTDdateFormat, offsetTimeFormat } from "@/utils/filters";
// 关注用户
import { userFollows, useUnfollows } from "@/apis/user";
// 评论
import commentFooter from "@/components/commentFooter";
export default {
  components: {
    commentFooter,
  },
  data() {
    return {
      axios,
      post: {
        // 2
        // user:{
        // }
      },
      comment: "", //评论
    };
  },

  // 过滤器注册
  filters: {
    YTDdateFormat,
    offsetTimeFormat,
  },
  //vue实例加载完，模板加载前
  async activated() {
    // 页面滚动到顶部
    window.scroll(0, 0);
    // 根据id获取文章的详情，实现文章详情的动态渲染
    // console.log(this.$route.params.id);
    // 异步请求一定要转同步
    let res = await getPostDetail(this.$route.params.id);
    console.log(res.data);
    this.post = res.data.data;
    console.log(this.post);
    // 获取文章评论
    let res2 = await commentlist(this.post.id);
    // console.log(res2);
    this.comment = res2.data.data;
    // 改造数据
    this.comment = this.comment.map((v) => {
      v.user.head_img = axios.defaults.baseURL + v.user.head_img;
      return v;
    });
    console.log(this.comment);
  },
  methods: {
    // 关注用户
    async user_follows() {
      let res;
      console.log(this.post.has_follow);
      try {
        if (this.post.has_follow) {
          // 关注
          res = await useUnfollows(this.post.user.id);
        } else {
          // 取消关注
          res = await userFollows(this.post.user.id);
        }
        // 实现页面的刷新
        this.post.has_follow = !this.post.has_follow;
        console.log(res);
        // 给用户提示信息
        this.$toast.success(res.data.message);
      } catch (error) {
        //捕捉错误，不做操作
      }
    },
    // 点赞
    async giveLike() {
      try {
        let res = await getLike(this.post.id);
        console.log(res);
        this.post.has_like = !this.post.has_like;
      } catch (error) {}
    },
  },
};
</script>

<style lang='less' scoped>
.articaldetail {
  margin-bottom: 54px;
}
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
  }
  .span {
    background-color: transparent;
    color: #000;
    border: 1px solid #ccc;
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    // 改变父组件里子组件的样式/deep/
    /deep/img {
      width: 100%;
      display: block;
    }
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  // 点赞
  .unlike {
    color: red;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>
